package com.egobuy.egobuyportal.controller;

import com.igeekhome.egobuy.content.pojo.TbContent;
import com.igeekhome.egobuy.feign.ContentClients;
import com.igeekhome.egobuy.util.ResponseEntity;
import com.igeekhome.egobuy.util.ResponseEntityV2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yadonghe
 * @date 2020-03-05 10:25
 */
@Controller
public class PageController {

    @Autowired
    private ContentClients contentClients;

    @Value("${BANNER_CONTENT_ID}")
    private Long banner_content_id;
    /**
     * 首页
     * @return
     */
    @RequestMapping("/")
    public String index(Model model) {
        //传递首页需要的数据
        //获取商城大广告 89
        //基于Feign实现声明式调用
        //ResponseEntityV2<TbContent>
        /*
        ResponseEntity responseEntity = contentClients.listContents(89L);
        System.out.println("responseEntity==>" + responseEntity);
        Object data = responseEntity.getData();//List->Object
        System.out.println("data==>" + data);
        List<TbContent> contents = new ArrayList<>();
        if (data != null && data instanceof List<?>.class) {
            for(Object o : (List<?>)data) {
                contents.add((TbContent)o);
            }

            model.addAttribute("ad1List", contents);
        }
         */
        ResponseEntityV2<TbContent> responseEntityV2 = contentClients.listContents(banner_content_id);
        List<TbContent> data = responseEntityV2.getData();
        model.addAttribute("ad1List", data);
        return "index";
    }
}
